package com.henushang.ranking.dao.impl;

import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.stereotype.Repository;

import com.henushang.ranking.constant.FieldConstant;
import com.henushang.ranking.dao.RankingDataDao;
import com.henushang.ranking.domain.RankingData;
import com.henushang.ranking.domain.WebsiteInfo;
import com.henushang.ranking.util.mongo.MongoUtil;
import com.mongodb.client.MongoCollection;

public abstract class BaseRankingDataDaoImpl implements RankingDataDao {

    private static MongoCollection<WebsiteInfo> coll = null;

    @Override
    public boolean insert(RankingData rankingData) {
        return insertOne(rankingData);
    }

    @Override
    public RankingData getOne(String _id) {

        return findOneById(_id);
    }

    @Override
    public boolean delete(String _id) {
        return deleteById(_id);
    }

    @Override
    public List<RankingData> getListByQueryMap(Map<String, Object> queryMap) {
        return findList(queryMap);
    }

    @Override
    public boolean update(RankingData rankingData) {
        return updateById(rankingData);
    }



    @Override
    public Document toDocument(RankingData rankingData) {
        Document doc = new Document();
        doc.append(FieldConstant.id, rankingData.get_id());
        doc.append(FieldConstant.createTime, rankingData.getCreate_time());
        doc.append("category_id", rankingData.getCategory_id());
        doc.append("site_id", rankingData.getSite_id());
        doc.append("desc", rankingData.getDesc());
        doc.append("url", rankingData.getUrl());
        doc.append("rank_num", rankingData.getRank_num());
        doc.append("content", rankingData.getContent());
        doc.append("has_content", rankingData.isHas_content());
        return doc;
    }

    @Override
    public RankingData fromDocument(Document document) {
        RankingData data = new RankingData();
        if (document == null) {
            return data;
        }
        data.set_id(document.getString("_id"));
        data.setCreate_time(document.getString("create_time"));
        data.setCategory_id(document.getString("category_id"));
        data.setSite_id(document.getString("site_id"));
        data.setDesc(document.getString("desc"));
        data.setUrl(document.getString("url"));
        data.setRank_num(document.getInteger("rank_num"));
        data.setContent(document.getString("content"));
        data.setHas_content(document.getBoolean("has_content"));
        return data;
    }

}
